import Vue from "vue";
import Vuex from "vuex";
import {
  RESOURCE_A_GETTER,
  RESOURCE_B_GETTER,
  RESOURCE_C_GETTER,
  UPDATE_RESOURCE,
  FETCH_RESOURCE_ACTION,
} from "@/constants/store";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    resourceA: [],
    resourceB: [],
    resourceC: [],
  },
  getters: {
    [RESOURCE_A_GETTER]: (state) => state.resourceA,
    [RESOURCE_B_GETTER]: (state) => state.resourceB,
    [RESOURCE_C_GETTER]: (state) => state.resourceC,
  },
  mutations: {
    [UPDATE_RESOURCE](state, { target, data }) {
      state[`resource${target}`] = data;
    },
  },
  actions: {
    [FETCH_RESOURCE_ACTION]({ commit }, { target, wait }) {
      const data = [{ foo: 1 }, { foo: 2 }, { foo: 3 }];
      return new Promise(function (resolve) {
        setTimeout(() => {
          commit(UPDATE_RESOURCE, { target, data });
          resolve(data);
        }, wait * 1000);
      });
    },
  },
});
